//
//  MARequestManager.m
//  MateAdv
//
//  Created by 茶 on 2023/9/8.
//

#import "MARequestManager.h"
#import "MANetworkTool.h"
#import "MAIAPTool.h"

@interface MARequestManager ()

@property (nonatomic, strong) MANetworkTool *netTool;

@end

@implementation MARequestManager

- (void)requestBaseInfo {
    MAWeakify(self);
    [self.netTool ma_post:@"/api/common/config" params:nil successed:^(id  _Nonnull object, BOOL isCache) {
        MAStrongify(self);
        [NSUserDefaults.standardUserDefaults setObject:object forKey:kAppInfo];
        [self fetchMealSuccessed:nil failed:nil];
    } failed:^(NSError * _Nonnull error) {
        
    }];
}

- (void)fetchUserInfoWithSuccessed:(nullable void (^)(id _Nonnull object))successed failed:(nullable void(^)(NSError * error))failed {
    [self.netTool ma_post:@"/api/user/info" params:nil successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)fetchRuleWithSuccessed:(void (^)(id _Nonnull))successed failed:(void (^)(NSError * _Nonnull))failed {
    [self.netTool ma_post:@"/api/game/list" params:nil successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)fetchMealSuccessed:(nullable void (^)(id _Nonnull))successed failed:(nullable void (^)(NSError * _Nonnull))failed{
    NSDictionary *dic = [NSUserDefaults.standardUserDefaults objectForKey:kAppInfo];
    NSDictionary *meal = [dic objectForKey:@"meal"];
    NSString *mealID = [meal objectForKey:@"basics_meal_id"];
    if (mealID) {
        [self.netTool ma_post:@"/api/order/meal" params:@{@"meal_id" : mealID} successed:^(id  _Nonnull object, BOOL isCache) {
            [NSUserDefaults.standardUserDefaults setObject:object forKey:kSavedMeal];
            [[MAIAPTool shareTool] ma_requestAllProductWithCompletion:nil];
            MABlock(successed, object);
        } failed:failed];
    }
}

- (void)loginWithType:(NSUInteger)type params:(NSDictionary *)params successed:(void (^)(id _Nonnull))successed failed:(void (^)(NSError * _Nonnull))failed {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@(type) forKey:@"type"];
    if (params) {
        [dic addEntriesFromDictionary:params];
    }
    [self.netTool ma_post:@"/api/user/login" params:dic successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)payVerifyReceiptnWithParams:(NSDictionary *)params successed:(void (^)(id _Nonnull))successed failed:(void (^)(NSError * _Nonnull))failed {
    [self.netTool ma_post:@"/api/notify/ApplePayVerifyReceipt" params:params successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)createOrderWithParams:(NSDictionary *)params successed:(void (^)(id _Nonnull))successed failed:(void (^)(NSError * _Nonnull))failed {
    [self.netTool ma_post:@"/api/order/create" params:params successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)logoutWithSuccessed:(void (^)(id _Nonnull))successed failed:(void (^)(NSError * _Nonnull))failed {
    [self.netTool ma_post:@"/api/user/logout" params:nil successed:^(id  _Nonnull object, BOOL isCache) {
        MABlock(successed, object);
    } failed:failed];
}

- (void)setUserUUID:(NSString *)userUUID {
    _userUUID = userUUID;
    self.netTool.userUUID = userUUID;
}

#pragma mark -- Singleton
+ (MARequestManager *)shareManager {
    static MARequestManager *manager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [self new];
    });
    return manager;
}

#pragma mark -- LazyLoading
- (MANetworkTool *)netTool {
    if (_netTool) return _netTool;
    _netTool = [MANetworkTool new];
    _netTool.jsonRequest = YES;
    _netTool.timeoutInterval = 30;
    return _netTool;
}

@end
